<template>
  <div class="review">
    <div class="wrapper">
      <i class="avatar"></i>
      <textarea
        v-model="comment"
        @focus="fs"
        @blur="leafs"
        @input="write"
        @keyup.enter="print"
        id="tx"
        placeholder="发一条友善的评论"
        rows="2"
        maxlength="200"
      ></textarea>
      <button>发布</button>
    </div>
    <div class="wrapper">
      <span class="total">0/200字</span>
    </div>
    <div class="list">
      <div class="item" v-for="(item, idx) in comments" :key="idx">
        <i class="avatar"></i>
        <div class="info">
          <p class="name">{{ item.username }}</p>
          <p class="text">{{ item.text }}</p>
          <p class="time">{{ item.time }}</p>
          <button @click="deleteComment(idx)" class="delete-btn">
            &nbsp;&nbsp;删除&nbsp;&nbsp;
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script  scoped>
export default {
  data() {
    // 优先查找 localStorage，没有再查 sessionStorage
    let userInfo =
      localStorage.getItem("userInfo") || sessionStorage.getItem("userInfo");
    userInfo = userInfo ? JSON.parse(userInfo) : {};
    return {
      username: userInfo.username || "",
      comment: "",
      comments: [],
    };
  },
  methods: {
    fs() {
      const total = document.querySelector(".total");
      total.style.opacity = 1;
    },
    leafs() {
      const total = document.querySelector(".total");
      total.style.opacity = 0;
    },
    print() {
      if (this.comment.trim()) {
        this.comments.unshift({
          username: this.username,
          text: this.comment,
          time: new Date().toLocaleString(),
        });
        this.comment = "";
        this.write();
      }
    },
    write() {
      const total = document.querySelector(".total");
      total.innerHTML = `${this.comment.length}/200字`;
    },
    deleteComment(idx) {
      this.comments.splice(idx, 1);
    },
  },
};
</script>

<style>
.review {
  width: 100%;
}
.wrapper {
  margin: 0 auto;
  min-width: 400px;
  max-width: 800px;
  display: flex;
  justify-content: flex-end;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  overflow: hidden;
  background: url(../../public/Me-headimg.png) no-repeat center / cover;
  margin-right: 20px;
}

.wrapper textarea {
  outline: none;
  border-color: transparent;
  resize: none;
  background: #f5f5f5;
  border-radius: 4px;
  flex: 1;
  padding: 10px;
  transition: all 0.5s;
  height: 30px;
}

.wrapper textarea:focus {
  border-color: #e4e4e4;
  background: #fff;
  height: 50px;
}

.wrapper button {
  background: #00aeec;
  color: #fff;
  border: none;
  border-radius: 4px;
  margin-left: 10px;
  width: 70px;
  cursor: pointer;
}

.wrapper .total {
  margin-right: 80px;
  color: #999;
  margin-top: 5px;
  opacity: 0;
  transition: all 0.5s;
}

.list {
  min-width: 400px;
  max-width: 800px;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 0 auto;
}

.list .item {
  width: 100%;
  display: flex;
  margin: 0, auto;
}

.list .item .info {
  flex: 1;
  border-bottom: 1px dashed #e4e4e4;
  padding-bottom: 10px;
  position: relative;
}

.list .item .delete-btn {
  position: absolute;
  right: 0;
  top: 10px;
  border: none;
  background: #ffa500;
  font-size: 19px;
  border-radius: 10px;
}
.list .item p {
  margin: 0;
}

.list .item .name {
  color: #fb7299;
  font-size: 14px;
  font-weight: bold;
}

.list .item .text {
  color: #333;
  padding: 10px 0;
}

.list .item .time {
  color: #999;
  font-size: 12px;
}
</style>